/**
 * 库文件管理路由
 *
 * Created by snowingsea on 2020/11/03.
 */
// const multer = require('multer');
// const cacheUtil = require('../../cacheUtil');
const middleware = require('./../middleware');
const uploadProgress = require('../middleware/upload-progress');

/**
 * 要求环境信息存在
 * @param req
 * @param res
 * @param next
 */
function requireEnvironmentExist(req, res, next) {
  req.$environment = req.$projectVersion;
  next();
}

/**
 * Export model definition object.
 */
module.exports = (app, prefix) => {
  const subFilesURL = `${prefix}/files/:fileId/files.json`; // 批量添加文件url
  const subFileURL = `${prefix}/files/:fileId/file.json`; // 单个添加/修改文件url
  const fileURL = `${prefix}/files/:fileId.json`;
  const fileChildrenURL = `${prefix}/files/:fileId/children.json`;
  const fileTreeURL = `${prefix}/files/:fileId/tree.json`;
  const upload = uploadProgress;

  // 获取文件信息
  app.get(fileURL, middleware.user.requireTokenUnexpired);
  app.get(fileURL, middleware.user.requireAvailable);
  app.get(fileURL, middleware.user.requireAdministrator);
  app.get(fileURL, middleware.file.requireFileExist);
  app.get(fileURL, middleware.file.getFile);

  // 获取子目录/文件列表
  app.get(fileChildrenURL, middleware.user.requireTokenUnexpired);
  app.get(fileChildrenURL, middleware.user.requireAvailable);
  app.get(fileChildrenURL, middleware.user.requireAdministrator);
  app.get(fileChildrenURL, middleware.file.requireFileExist);
  app.get(fileChildrenURL, middleware.file.getFileChildren);

  // 获取目录树结构
  app.get(fileTreeURL, middleware.user.requireTokenUnexpired);
  app.get(fileTreeURL, middleware.user.requireAvailable);
  app.get(fileTreeURL, middleware.user.requireAdministrator);
  app.get(fileTreeURL, middleware.file.requireFileExist);
  app.get(fileTreeURL, middleware.file.getFileTree);

  // 添加文件/文件夹
  app.post(subFileURL, middleware.user.requireTokenUnexpired);
  app.post(subFileURL, middleware.user.requireAvailable);
  app.post(subFileURL, middleware.user.requireAdministrator);
  app.post(subFileURL, middleware.file.requireFileExist);
  app.post(subFileURL, middleware.user.checkVersionLock);
  app.post(subFileURL, middleware.file.requireDirectory);
  app.post(subFileURL, requireEnvironmentExist);
  app.post(subFileURL, middleware.user.getSuperUser);
  app.post(subFileURL, middleware.progressNotify());
  app.post(subFileURL, upload.single('file'));
  app.post(subFileURL, middleware.user.getLockByVersion({ enabled: true, expireSec: 300 }));
  app.post(subFileURL, middleware.file.addFile);
  app.post(subFileURL, middleware.file.updateFiles);

  // 批量添加文件
  app.post(subFilesURL, middleware.user.requireTokenUnexpired);
  app.post(subFilesURL, middleware.user.requireAvailable);
  app.post(subFilesURL, middleware.user.requireAdministrator);
  app.post(subFilesURL, middleware.file.requireFileExist);
  app.post(subFilesURL, middleware.user.checkVersionLock);
  app.post(subFilesURL, middleware.file.requireDirectory);
  app.post(subFilesURL, requireEnvironmentExist);
  app.post(subFilesURL, middleware.user.getSuperUser);
  app.post(subFilesURL, middleware.progressNotify());
  app.post(subFilesURL, upload.array('files'));
  app.post(subFilesURL, middleware.user.getLockByVersion({ enabled: true, expireSec: 300 }));
  app.post(subFilesURL, middleware.file.addFiles);

  // 更新文件/文件夹
  app.put(subFileURL, middleware.user.requireTokenUnexpired);
  app.put(subFileURL, middleware.user.requireAvailable);
  app.put(subFileURL, middleware.user.requireAdministrator);
  app.put(subFileURL, middleware.file.requireFileExist);
  app.put(subFileURL, middleware.user.checkVersionLock);
  app.put(subFileURL, requireEnvironmentExist);
  app.put(subFileURL, middleware.user.getSuperUser);
  app.put(subFileURL, middleware.progressNotify());
  app.put(subFileURL, upload.single('file'));
  app.put(subFileURL, middleware.user.getLockByVersion({ enabled: true, expireSec: 300 }));
  app.put(subFileURL, middleware.file.updateFiles);

  // 删除文件/文件夹
  app.delete(fileURL, middleware.user.requireTokenUnexpired);
  app.delete(fileURL, middleware.user.requireAvailable);
  app.delete(fileURL, middleware.user.requireAdministrator);
  app.delete(fileURL, middleware.user.getSuperUser);
  app.delete(fileURL, middleware.file.requireFileExist);
  app.delete(fileURL, middleware.user.getLockByVersion({ enabled: true, expireSec: 300 }));
  app.delete(fileURL, requireEnvironmentExist);
  app.delete(fileURL, middleware.file.removeFile);

  // 编辑项目文件
  app.put(fileURL, middleware.user.requireTokenUnexpired);
  app.put(fileURL, middleware.user.requireAvailable);
  app.put(fileURL, middleware.user.requireAdministrator);
  app.put(fileURL, middleware.user.getSuperUser);
  app.put(fileURL, middleware.file.requireFileExist);
  app.put(fileURL, middleware.user.getLockByVersion({ enabled: true, expireSec: 300 }));
  app.put(fileURL, requireEnvironmentExist);
  app.put(fileURL, middleware.file.editFile);
};
